package com.example.finalapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.parse.Parse;

public class MainActivity extends Activity {
	EditText editTextEmail;
	EditText editTextPass;
	Boolean isEmpty;
	Boolean isEmailValid;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
		 
		Parse.initialize(this, "6xDnXjWOcfFRJMlrIJxdVnvtjlTqRgtq8VnSh9Qn", "cxSxdJr5nke0qSxPAIYgU8TwLmzsXZnyzM5U4z8t");
		
		editTextEmail = (EditText)findViewById(R.id.editTextEmailLogin);
		editTextPass = (EditText)findViewById(R.id.editTextPasswordLogin);
//		isEmpty=false;
//		isEmailValid=true;
		
        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {
 
            public void onClick(View v) {
                // Switching to Register screen
        		Intent intent = new Intent(MainActivity.this, RegistrationActivity.class);
                startActivity(intent);
            }
        });
        
        final Button button = (Button) findViewById(R.id.btnLogin);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
//            	if(MainActivity.this.editTextEmail.getText().toString().trim().equals("")){
//            		isEmpty=true;
//            	}else if(!((MainActivity.this.editTextEmail.getText().toString().trim().contains("@"))
//               			 & (MainActivity.this.editTextEmail.getText().toString().trim().contains(".")))){
//               		isEmailValid=false;
//               		Toast.makeText(MainActivity.this, "Your Email is not legitimite",Toast.LENGTH_LONG).show();
//               	}
//            	if(MainActivity.this.editTextPass.getText().toString().trim().equals("")){
//            		isEmpty=true;
//            	}
//            	if (isEmailValid){
//            		if(isEmpty){
//                		Toast.makeText(MainActivity.this, "At least one of your fields is empty!",Toast.LENGTH_LONG).show();
//                		isEmpty=false;
//            		}else{
//			        	 Log.d("else", "before");
//
//            			ParseQuery<ParseObject> query = ParseQuery.getQuery("roommate");
//            			query.whereEqualTo("Email", MainActivity.this.editTextEmail.getText().toString());
//            			query.findInBackground(new FindCallback<ParseObject>() {
//            			    public void done(List<ParseObject> roomie, ParseException e) {
//            			        if (e == null) {
//            			        	 Log.d("score", "Retrieved " + roomie.size() + " scores");
//            			        	String passText = roomie.get(0).getString("Password").toString();
//            			        	if (passText.equals(MainActivity.this.editTextPass.getText().toString()))
//            			        	{
//            			        		// Switching to Home Screen
//            			            	Intent intent = new Intent(MainActivity.this, HomeActivity.class);
//            			            	startActivity(intent);
//            			        	}else{
//            	                		Toast.makeText(MainActivity.this, "Incorrect email or password",Toast.LENGTH_LONG).show();
//            			        	}
//            			        } else {
//            			            Log.d("roomie", "Error: " + e.getMessage());
//            			        }
//            			    }
//            			});
//            		}
//            	}
            	
            	
            }
        });
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


}
